<?php
/**
 * 后台主页控制器
 * User: ws
 * Date: 2018/7/20
 * Time: 14:39
 */
namespace app\merchant\controller;

use think\Db;
class Index extends Init
{
    function _initialize()
    {
        parent::_initialize();

    }



    function index()
    {
        return  view('index');
    }

    function home()
    {
        $id = $_SESSION['think']['admin_user']['store_id'];


        //$where['pay_time'] = array(array('egt',$today_start),array('elt',$today_end)) ;
        $where['store_id'] = $id;

        $where['type'] = 2;//销售订单
        $list['order_count1']= Db::name('order')->whereTime('pay_time', 'd')->where($where)->count();
        $list['pay_price1']= Db::name('order')->whereTime('pay_time', 'd')->where($where)->sum('pay_price');

        $where['type'] = 1;//会员订单
        $list['order_count2']= Db::name('order')->whereTime('pay_time', 'd')->where($where)->count();
        $list['pay_price2']= Db::name('order')->whereTime('pay_time', 'd')->where($where)->sum('pay_price');

        //昨天
        unset($where['pay_time']);
        $where['type'] = 2;//销售订单
        //$where['pay_time'] = array(array('egt',$yesterday_start),array('elt',$yesterday_end)) ;
        $list2['order_count1']= Db::name('order')->whereTime('pay_time', 'yesterday')->where($where)->count();
        $list2['pay_price1']= Db::name('order')->whereTime('pay_time', 'yesterday')->where($where)->sum('pay_price');

        $where['type'] = 1;//会员订单
        $list2['order_count2']= Db::name('order')->whereTime('pay_time', 'yesterday')->where($where)->count();
        $list2['pay_price2']= Db::name('order')->whereTime('pay_time', 'yesterday')->where($where)->sum('pay_price');

        //本月
        unset($where['pay_time']);
        $where['type'] = 2;//销售订单
        //$where['pay_time'] = array(array('egt',$thismonth_start),array('elt',$thismonth_end)) ;
        $list3['order_count1']= Db::name('order')->whereTime('pay_time', 'm')->where($where)->count();
        $list3['pay_price1']= Db::name('order')->whereTime('pay_time', 'm')->where($where)->sum('pay_price');

        $where['type'] = 1;//会员订单
        $list3['order_count2']= Db::name('order')->whereTime('pay_time', 'm')->where($where)->count();
        $list3['pay_price2']= Db::name('order')->whereTime('pay_time', 'm')->where($where)->sum('pay_price');

        //本年
        unset($where['pay_time']);
        $where['type'] = 2;//销售订单
        //$where['pay_time'] = array(array('egt',$year_start),array('elt',$year_end)) ;
        $list4['order_count1']= Db::name('order')->whereTime('pay_time', 'y')->where($where)->count();
        $list4['pay_price1']= Db::name('order')->whereTime('pay_time', 'y')->where($where)->sum('pay_price');

        $where['type'] = 1;//会员订单
        $list4['order_count2']= Db::name('order')->whereTime('pay_time', 'y')->where($where)->count();
        $list4['pay_price2']= Db::name('order')->whereTime('pay_time', 'y')->where($where)->sum('pay_price');


        unset($where);
        //$where['pay_time'] = array(array('egt',$today_start),array('elt',$today_end)) ;
        $where['u.store_id'] = $id;
        $list['order_count3'] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->whereTime('pay_time', 'd')->where($where)->count();
        $list['pay_price3'] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->whereTime('pay_time', 'd')->where($where)->sum('pay_price');

        //昨天
        unset($where);
        //$where['pay_time'] = array(array('egt',$yesterday_start),array('elt',$yesterday_end)) ;
        $where['u.store_id'] = $id;
        $list2['order_count3'] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->whereTime('pay_time', 'yesterday')->where($where)->count();
        $list2['pay_price3'] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->whereTime('pay_time', 'yesterday')->where($where)->sum('pay_price');

        //本月
        unset($where);
        //$where['pay_time'] = array(array('egt',$thismonth_start),array('elt',$thismonth_end)) ;
        $where['u.store_id'] = $id;
        $list3['order_count3'] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->whereTime('pay_time', 'm')->where($where)->count();
        $list3['pay_price3'] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->whereTime('pay_time', 'm')->where($where)->sum('pay_price');

        //本年
        unset($where);
        //$where['pay_time'] = array(array('egt',$year_start),array('elt',$year_end)) ;
        $where['u.store_id'] = $id;
        $list4['order_count3'] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->whereTime('pay_time', 'y')->where($where)->count();
        $list4['pay_price3'] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->whereTime('pay_time', 'y')->where($where)->sum('pay_price');


        //商品信息
        $map['store_id'] = $id;
        $goods['num']= Db::name('goods')->where($map)->count();

        $map['sale_type'] = 2;
        $goods['type_num']= Db::name('goods')->where($map)->count();

        unset($map['sale_type']);
        $map['shelves'] = 0;
        $goods['shelves_num']= Db::name('goods')->where($map)->count();


        unset($map['shelves']);
        $map['state'] = 2;
        $map['store_id'] = $id;
        $goods['shortage_num'] = Db::table('je_goods')->alias('g')->join('je_spec_goods_price s','g.goods_id = s.goods_id')->where(['g.store_id'=>$id,'s.store'=>0])->count();


        //订单
        unset($map['state']);
        $map['order_status'] = 1;
        $map['type'] = 2;
        $order['order_status']= Db::name('order')->where($map)->count();

        unset($map['type']);
        $map['type'] = 1;
        $order['order_status2']= Db::name('order')->where($map)->count();

        $map['order_status'] = 5;
        $order['order_status3']= Db::name('order')->where($map)->count();

        $map['order_status'] = 10;
        $order['order_status4']= Db::name('order')->where($map)->count();
        return  view('home',['list'=>$list,'list2'=>$list2,'list3'=>$list3,'list4'=>$list4,'goods'=>$goods,'order'=>$order]);
    }


    function echarts(){
        $current_year  = (int) date('Y', time()); // 当前年份
        $current_month = (int) date('m', time()); // 当前月份


        $list3[0] =   ['product', '销售收入', '共享收入', '会员卡收入'];
        for ($i = 01; $i <= $current_month; $i++) {
             $thismonth_start=mktime(0,0,0,date($i),1,date('Y'));
             $thismonth_end=mktime(23,59,59,date($i),date('t'),date('Y'));

             $list3[$i][] = $current_year.date('m',$thismonth_start);;

            $where['store_id'] = $_SESSION['think']['admin_user']['store_id'];
            $where['type'] = 2;//销售订单
            $where['pay_time'] = array(array('egt',$thismonth_start),array('elt',$thismonth_end)) ;

            $list3[$i][]= Db::name('order')->where($where)->sum('pay_price');

            $where['type'] = 1;//会员订单
            $list3[$i][]= Db::name('order')->where($where)->sum('pay_price');


            unset($where);
            $where['pay_time'] = array(array('egt',$thismonth_start),array('elt',$thismonth_end)) ;
            $where['u.store_id'] = $_SESSION['think']['admin_user']['store_id'];
            $where['card_status'] = 1;
            $list3[$i][] = Db::table('je_card_order')->alias('o')->join('je_store_user s','o.store_user_sn = s.sn')->join('je_store u','s.store_id = u.store_id')->where($where)->sum('pay_price');
                unset($where);
        }

        $list['data'] = $list3;
        echo json_encode($list);die;
    }

}
